"======================全局插件=======================

"(1)TagList
"不同时显示多个文件的tag,只显示当前文件的tag
let Tlist_Show_One_File=1
"如果taglist窗口是最后一个窗口，则退出vim
let Tlist_Exit_OnlyWindow=1

"(2)winManager
let g:winManagerWindowLayout='FileExplorer|TagList'

"(3)acp
let g:acp_enableAtStartup = 1
" 自动完成设置 禁止在插入模式移动的时候出现 Complete 提示
"let g:acp_mappingDriven = 1
let g:acp_ignorecaseOption = 0
" 尝试补全关键字所需的（光标前）字符数量。如果为负数，则不启用关键字补全。
let g:acp_behaviorKeywordLength = 3
" 自动完成设置为 Ctrl + p 弹出
let g:acp_behaviorKeywordCommand = "\<C-p>"
"let g:acp_completeoptPreview = 0
let g:acp_behaviorKeywordIgnores = ['for','if','else','int','double','char']

"(4)super-tab
"使用TAB进行补全 
"可以补全 include 文件里面的名称
let g:SuperTabDefaultCompletionType="<C-X><C-O>"

"(6) DoxygenToolkit
let g:DoxygenToolkit_commentType = "C++"
let g:DoxygenToolkit_briefTag_pre="@brief "
let g:DoxygenToolkit_paramTag_pre="@Param "
let g:DoxygenToolkit_returnTag="@Return   "
"let g:DoxygenToolkit_blockHeader="--------------------------------------------------------------------------"
"let g:DoxygenToolkit_blockFooter="----------------------------------------------------------------------------"
let g:DoxygenToolkit_authorName="Mathias Lorente"
let g:DoxygenToolkit_licenseTag="My own license"  

"(7)MiniBufExplorer 
"<Tab>  向前循环切换到每个buffer名上
"<S-Tab>    向后循环切换到每个buffer名上
"<Enter>    在打开光标所在的buffer
"d  删除光标所在的buffer
" 在GVIM下才有效 C+TAB切换当前编辑文件
let g:miniBufExplMapCTabSwitchBufs = 1

"=================================================

"Mark .asm files NASM-type assembly
au BufNewFile,BufReadPre *.asm let b:asmsyntax='nasm'

"每当文件保存时，就自动更新当前目录的tags
autocmd! bufwritepost *.c,*.h,*.cpp,*.cc call Update_current_tags()

func! Update_current_tags()
	silent ! ctags -R -F -B --c++-kinds=+p --fields=+iaS --extra=+q .
endfunc

"==================general========================
"不兼容vi模式，否则会出现很多情况
set nocompatible
set ts=4
set expandtab
syntax enable
syntax on
set modeline
set fileencodings=ucs-bom,utf-8,gb18030,cp936,big5,euc-jp,sjis,euc-kr,ucs-2le,latin1
set shiftwidth=4
set cindent
"设置忽略大小写，比如在搜索的时候
set ignorecase
"设置鼠标为可行
set mouse=a
"代码高亮所用的配色方案
colorscheme evening
"google
set backspace=indent,eol,start
"不产生备份文件
set nobackup   
"命令行的历史记录数
set history=50    
"显示光标状态
set ruler 
"show the command being typed
set showcmd
"查询时非常方便，如要查找book单词，当输入到/b时，会自动找到第一个b开头的单词
set incsearch   
"搜索时高亮显示找到的文本
set hlsearch
"永远显示状态行
set laststatus=2
"设置状态行显示的内容
set statusline=\ %F%m%r\ \ \ %{getcwd()}%h\ \ \ Line:\ %l/%L:%c

"======================================================

if has("autocmd")
  "load filetype plugins/indent settings
  filetype plugin indent on
  set cindent
  augroup vimrcEx
  au!
  autocmd BufReadPost *
    \ if line("'\"") > 0 && line("'\"") <= line("$") |
    \  exe "normal! g`\"" |
    \ endif
  augroup END
else
  set autoindent
endif " has("autocmd")

"==========================各语言设置===================================
autocmd FileType c call CSET()
autocmd FileType cpp call CPPSET()
autocmd FileType python call PYSET()
autocmd FileType php call PHPSET()
autocmd FileType java call JAVASET()
autocmd FileType sh call SHELLSET()
autocmd BufNewFile,BufReadPre *.wiki call WIKISET()
autocmd FileType make set noexpandtab

func! WIKISET()
    " 使用鼠标映射
    let g:vimwiki_use_mouse = 1
    " 不要将驼峰式词组作为 Wiki 词条
    let g:vimwiki_camel_case = 0
    " 声明可以在wiki里面使用的HTML标签
    let g:vimwiki_valid_html_tags='b,i,s,u,sub,sup,kbd,br,hr,div,del,code,red,center,left,right,h4,h5,h6,pre'
    let g:vimwiki_list = [{'path': '~/.vim/mysite/wiki',
    \ 'path_html': '~/.vim/mysite/html/',
    \ 'template_path': '~/.vim/mysite/template/',
    \ 'template_default': 'header',
    \ 'template_ext': '.html',
    \ 'auto_export': 1,
    \ 'nested_syntaxes': {'Asm': 'asm', 'C': 'c', 'C++': 'cpp','Java': 'java', 'Haskell': 'haskell', 'Lua': 'lua', 'Perl': 'perl', 'Python': 'python', 'Ruby': 'ruby', 'PHP': 'php', 'HTML': 'html', 'Bash': 'sh', 'Vim': 'vim', 'Make': 'make', },}]
endfunc

func! SHELLSET()
    map <F9> :!gnome-terminal --command="bash -c '\"%\"; read -p \"  Press Enter...\"'"&<cr><cr><C-L>
    map <C-F9> <F9>
endfunc

function! CustomCodeAssistInsertMode()
    call RopeCodeAssistInsertMode()
    if pumvisible()
        return "\<C-L>\<Down>"
    else
        return ''
    endif
endfunction
 
function! TabWrapperComplete()
    let cursyn = synID(line('.'), col('.') - 1, 1)
    if pumvisible()
        return "\<C-Y>"
    endif
    if strpart(getline('.'), 0, col('.')-1) =~ '^\s*$' || cursyn != 0
        return "\<Tab>"
    else
        return "\<C-R>=CustomCodeAssistInsertMode()\<CR>"
    endif
endfunction
  
func! PYSET()
    if has('win32')
        map <F9> :w!<cr>:!start cmd /c "python % && pause \|\| pause"<cr><cr><C-L>
    else
        map <F9> :!gnome-terminal --command="bash -c 'python \"%\"; read -p \" Press Enter...\"'"&<cr><cr><C-L>
    endif
    map <C-F9> <esc><F9>
    set expandtab
    setlocal omnifunc=pythoncomplete#Complete 
    setlocal completefunc=pythoncomplete#CompleteParamsInfo

    let $PYTHONPATH .= ":~/.vim/ropehg/rope/:~/.vim/ropehg/rope_mode/:~/.vim/ropehg/rope_vim/"
    source ~/.vim/ropehg/rope_vim/ropevim.vim
    let ropevim_codeassist_maxfixes=10
    let ropevim_guess_project=1
    let ropevim_vim_completion=1
    let ropevim_enable_autoimport=1
    let ropevim_extended_complete=1
    inoremap <buffer><silent><expr> <Tab> TabWrapperComplete()
 
    if has('win32')
        "setlocal dict+=~/complete-dict
    else
        "setlocal dict+=~/.vim/dict/complete-dict
    endif
endfunc

func! PHPSET()
    if has('win32')
        map <F9> :!start cmd /c "php % && pause \|\| pause"<cr><cr><C-L>
    else
        map <F9> :!gnome-terminal --command="bash -c 'php \"%\"; read -p \" Press Enter...\"'"&<cr><cr><C-L>
    endif
    map <C-F9> <esc><F9>
    setlocal omnifunc=phpcomplete#CompletePHP
    if has('win32')
        setlocal dict+=~/dict/php_funclist
    else
        setlocal dict+=~/.vim/dict/php_funclist.txt
    endif
endfunc

func! JAVASET()
    set makeprg=javac\ %
    map <F9> :w!<cr>:make<cr><C-L>:cl<cr>
    if has('win32')
        map <C-F9> :!start cmd /c "java %< && pause \|\| pause"<cr><cr><C-L>
    else
        map <C-F9> :!gnome-terminal --command="bash -c 'time java \"%<\"; read -p \" Press Enter...\"'"&<cr><cr><C-L>
    endif
    " 用于代码补全,修改code_complete使用的快捷键，使super-tab可以顺利生效
    " let g:completekey = '`' code_complete已经被移除了
    setlocal omnifunc=javacomplete#Complete
    setlocal completefunc=javacomplete#CompleteParamsInfo
endfunc

func! CSET()
    call CPPSET()
    if has('win32')
        set makeprg=gcc\ -Wall\ -g\ -o\ %<.exe\ %
    else
        set makeprg=gcc\ -Wall\ -g\ -o\ %<.out\ %
    endif 
endfunc

func! CPPSET()

    "(1) omnicppcomplete 
    "1 依赖ctags,
    "2 需要using其命名空间,类前面不要加命名空间，否则无法补足
    "3 类的成员变量，离开类的声明区域就无法进行补全(实际上是omni不知道其存在)

    "让omnicppcomplete关闭预览窗口，默认是开启的
    set completeopt=menu,longest
    let OmniCpp_DisplayMode=1
    let OmniCpp_ShowPrototypeInAbbr=1
    let OmniCpp_NamespaceSearch=2
    let OmniCpp_MayCompleteScope=1
    
    "(2) code_complete
    "使用TAB进行触发
    "依赖tags,如果想更新变化，必须产生新的tags. 
    
    "(3) a.vim 头文件与实现间的跳转
    let g:alternateExtensions_h = "cc,CC,cpp,c,cxx"

    "(4) cscope
    "在项目根目录cscope -Rbq
    cs add ./cscope.out .
    set cscopequickfix=s-,c-,d-,i-,t-,e-

    "(5) clang 
    "let g:clang_user_options = '-I/usr/local/include/ || exit 0'
    "let g:clang_snippets = 1
    "let g:clang_snippets_engine = 'snipmate'
    "let g:clang_complete_macros = 1
    "let g:completekey = '`'

    if has('win32')
        set makeprg=g++\ -Wall\ -g\ -o\ %<.exe\ %
        map <C-F9> :!start cmd /c "%<.exe && pause \|\| pause"<cr><cr><C-L>
        map <F8> :!gdb %<.exe<cr><C-L>
    else
        set makeprg=g++\ -Wall\ -g\ -o\ %<.out\ %
        map <C-F9> :!gnome-terminal --command="bash -c 'time \"./%<.out\"; read -p \" Press Enter...\"'"&<cr><cr><C-L>
        map <F8> :!gdb %<.out<cr><C-L>
        map <F7> :!make <cr>
        "加入stl中函数tags,以配合OmniCppComplete插件，使用ctags -R --c++-kinds=+p --fields=+iaS --extra=+q 
        "在/usr/include/c++/4.4.5/目录中生成tags,然后移动到我们指定目录里
        set tags+=~/.vim/tags/systags
        set tags+=~/.vim/tags/google_tags
        set tags+=~/.vim/tags/c++_tags
        set tags+=~/.vim/tags/base_tags
        set tags+=~/.vim/tags/ext_libs_tags
        set tags+=~/.vim/tags/file_tags
        set tags+=~/.vim/tags/indexing_tags
        set tags+=~/.vim/tags/infrastructure_tags
        set tags+=~/.vim/tags/util_tags
        set tags+=~/.vim/tags/web_util_tags
    endif
    set tags+=../tags
    set tags+=../../tags

    map <F9> :w!<cr>:make<cr><C-L>:cl<cr>
    " quickfix映射
    map <C-w> :cw<cr>
    map <C-n> :cn<cr>
    map <C-p> :cp<cr>

    " Dox映射
    map <C-d> :Dox<cr>
    imap <C-d> <Esc>:Dox<cr>

    " A.vim映射
    map <C-a> :A<cr>
    imap <C-a> <Esc>:A<cr>
endfunc

"========================全局映射===========================
"F2
map <F2> :WMToggle<cr>

imap <F3> <Esc><F3>
imap <F4> <Esc><F4>
imap <F5> <Esc><F5>
imap <F6> <Esc><F6>
imap <F7> <Esc><F7>
imap <F8> <Esc><F8>
imap <F9> <Esc><F9>
imap <C-F9> <Esc><F9>

" ,n for no highlight search(temporary)
nmap <leader>n :noh<cr>

"F5 for comment
vmap <F5> :s=^\(//\)*=//=g<cr>:noh<cr>
nmap <F5> :s=^\(//\)*=//=g<cr>:noh<cr>
imap <F5> <ESC>:s=^\(//\)*=//=g<cr>:noh<cr>
""F6 for uncomment
vmap <F6> :s=^\(//\)*==g<cr>:noh<cr>
nmap <F6> :s=^\(//\)*==g<cr>:noh<cr>
imap <F6> <ESC>:s=^\(//\)*==g<cr>:noh<cr>

